# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField

class AdminRoleDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertAdminRole(self, info):
        """
        添加admin_role信息
        :param info:
        :return:
        """
        roleName = info['roleName']
        roleType = info['roleType']
        roleDesc = info['roleDesc']
        roleIsSys = info['roleIsSys']

        sql = "insert into zzh_admin_role (role_name, role_type,role_desc, role_is_sys) VALUES " \
              "(%s,%s,%s,%s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [roleName, roleType, roleDesc, roleIsSys])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findAdminRoleById(self, roleId):
        """
        根据ID查询admin_role信息
        :param roleId:
        :return:
        """
        sql = "select * from zzh_admin_role WHERE role_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [roleId])
        roleJson = None
        if rs:
            list0 = cur.fetchall()
            roleJson = Table2Json.admin_role_list_to_json(list0)
        cur.close()
        self.conn.close()
        return roleJson

    def findAdminRoleByConditon(self, **kwargs):
        """
        admin_role表的条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        fieldMap = requestFieldMapToDBField.adminRoleFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM zzh_admin_role WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM zzh_admin_role WHERE " + whereSentence
        total_count = cur.execute(sql)

        if total_count:
            adinfo = cur.fetchall()
            lists = MyPaging().getPage(total_count, pageIndex, pageSize, adinfo)
            adJson = Table2Json.admin_role_list_to_json(lists)
            return adJson, total_count
        else:
            return None, 0

    def deleteAdminRole(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_admin_role WHERE role_id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateAdminRole(self, info):
        """
        更新admin_role
        :param info:
        :return:
        """
        roleId = info['roleId']
        roleName = info['roleName']
        roleType = info['roleType']
        roleDesc = info['roleDesc']
        roleIsSys = info['roleIsSys']

        sql = "update zzh_admin_role set role_name=%s,role_type=%s,role_desc=%s,role_is_sys=%s WHERE role_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [roleName, roleType, roleDesc, roleIsSys, roleId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectAdminRole(self,roleId):
        """
        根据ID查询，返回元组
        :param roleId:
        :return: data tuple
        """
        sql = "select * from zzh_admin_role WHERE role_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [roleId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()

        return arr
